﻿namespace MDXStudio
{
    using MDXParser;
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Windows.Forms;
    using WeifenLuo.WinFormsUI.Docking;

    internal class MessageLog
    {
        private DockContent m_DC;
        private bool m_Enabled;
        private DataGridView m_GridMessages;

        public MessageLog(DataGridView d, DockContent dc)
        {
            this.m_GridMessages = d;
            this.m_DC = dc;
            this.m_Enabled = true;
        }

        public void Clear()
        {
            if (this.m_Enabled)
            {
                this.m_GridMessages.Rows.Clear();
            }
        }

        private void OnMessagesContextMenuClick(object sender, EventArgs e)
        {
            ToolStripMenuItem item = (ToolStripMenuItem) sender;
            string name = item.Name;
            if (name != null)
            {
                if (!(name == "mItemHide"))
                {
                    if (!(name == "mItemHideAll"))
                    {
                        if (name == "mItemHelp")
                        {
                            Process.Start(item.Tag.ToString());
                        }
                        return;
                    }
                }
                else
                {
                    DataGridViewRow row = item.Tag as DataGridViewRow;
                    DataGridView view = row.DataGridView;
                    if (view.SelectedRows.Count > 0)
                    {
                        foreach (DataGridViewRow row2 in view.SelectedRows)
                        {
                            if (row2.Tag != null)
                            {
                                (row2.Tag as MDXParser.Message).Hide = true;
                            }
                        }
                    }
                    else if (row.Tag != null)
                    {
                        (row.Tag as MDXParser.Message).Hide = true;
                    }
                    this.Populate(view.Tag as MessageCollection);
                    return;
                }
                DataGridViewRow tag = item.Tag as DataGridViewRow;
                DataGridView dataGridView = tag.DataGridView;
                if (tag.Tag != null)
                {
                    int id = (tag.Tag as MDXParser.Message).Id;
                    foreach (DataGridViewRow row4 in (IEnumerable) dataGridView.Rows)
                    {
                        if (row4.Tag != null)
                        {
                            MDXParser.Message message = row4.Tag as MDXParser.Message;
                            if (message.Id == id)
                            {
                                message.Hide = true;
                            }
                        }
                    }
                    this.Populate(dataGridView.Tag as MessageCollection);
                }
            }
        }

        public void Populate(MessageCollection messages)
        {
            if (this.m_Enabled)
            {
                DataGridViewColumn sortedColumn = this.m_GridMessages.SortedColumn;
                SortOrder sortOrder = this.m_GridMessages.SortOrder;
                this.Clear();
                this.m_GridMessages.Tag = messages;
                foreach (MDXParser.Message message in messages)
                {
                    if (!message.Hide)
                    {
                        DataGridViewRow dataGridViewRow = new DataGridViewRow();
                        dataGridViewRow.CreateCells(this.m_GridMessages);
                        dataGridViewRow.Cells[0].Value = message.Severity;
                        if (message.Node != null)
                        {
                            dataGridViewRow.Cells[1].Value = message.Node.Locator.Line;
                            dataGridViewRow.Cells[2].Value = message.Node.Locator.Column;
                        }
                        dataGridViewRow.Cells[3].Value = message.Text;
                        if (message.URL != null)
                        {
                            dataGridViewRow.Cells[4].Value = "More info";
                            dataGridViewRow.Cells[4].Tag = message.URL;
                        }
                        dataGridViewRow.Tag = message;
                        ContextMenuStrip strip = new ContextMenuStrip();
                        ToolStripMenuItem item = new ToolStripMenuItem("Hide this message", null, new EventHandler(this.OnMessagesContextMenuClick), "mItemHide");
                        item.Tag = dataGridViewRow;
                        strip.Items.Add(item);
                        item = new ToolStripMenuItem("Hide all messages of this type", null, new EventHandler(this.OnMessagesContextMenuClick), "mItemHideAll");
                        item.Tag = dataGridViewRow;
                        strip.Items.Add(item);
                        if (message.URL != null)
                        {
                            item = new ToolStripMenuItem("Show help", null, new EventHandler(this.OnMessagesContextMenuClick), "mItemHelp");
                            item.Tag = message.URL;
                            strip.Items.Add(item);
                        }
                        dataGridViewRow.ContextMenuStrip = strip;
                        this.m_GridMessages.Rows.Add(dataGridViewRow);
                    }
                }
                if (sortOrder != SortOrder.None)
                {
                    ListSortDirection ascending = ListSortDirection.Ascending;
                    if (sortOrder == SortOrder.Descending)
                    {
                        ascending = ListSortDirection.Descending;
                    }
                    this.m_GridMessages.Sort(sortedColumn, ascending);
                }
                this.m_DC.Show();
            }
        }

        public void WriteErrorMessage(string message)
        {
            if (this.m_Enabled)
            {
                this.WriteMessage(message);
                this.m_DC.Show();
            }
        }

        public void WriteMessage(MDXParser.Message m)
        {
            if (this.m_Enabled)
            {
                DataGridViewRow dataGridViewRow = new DataGridViewRow();
                dataGridViewRow.CreateCells(this.m_GridMessages);
                dataGridViewRow.Cells[1].Value = m.Location.Line;
                dataGridViewRow.Cells[2].Value = m.Location.Column;
                dataGridViewRow.Cells[3].Value = m.Text;
                dataGridViewRow.Tag = m;
                this.m_GridMessages.Rows.Add(dataGridViewRow);
            }
        }

        public void WriteMessage(string message)
        {
            if (this.m_Enabled)
            {
                DataGridViewRow dataGridViewRow = new DataGridViewRow();
                dataGridViewRow.CreateCells(this.m_GridMessages);
                dataGridViewRow.Cells[3].Value = message;
                this.m_GridMessages.Rows.Add(dataGridViewRow);
            }
        }

        public void WriteMessage(int line, int col, string message)
        {
            if (this.m_Enabled)
            {
                DataGridViewRow dataGridViewRow = new DataGridViewRow();
                dataGridViewRow.CreateCells(this.m_GridMessages);
                dataGridViewRow.Cells[1].Value = line;
                dataGridViewRow.Cells[2].Value = col;
                dataGridViewRow.Cells[3].Value = message;
                this.m_GridMessages.Rows.Add(dataGridViewRow);
            }
        }

        public bool Enabled
        {
            get
            {
                return this.m_Enabled;
            }
            set
            {
                this.m_Enabled = value;
            }
        }
    }
}

